/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.chipchip.controller;

import com.chipchip.objects.CCMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import com.chipchip.objects.TAttributes;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

/*
 *
 * @author Tri
 */
public class MessageController extends ChannelInboundHandlerAdapter{
    private static final String NEWLINE = "\r\n";
    private GlobalNetworkHandler networkHandler = GlobalNetworkHandler.sharedInstance();
    
    /**
     * Phương thức dùng để nhận message
     * @param ctx channel đang nhận message
     * @param msg message được nhận
     * @throws Exception 
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg); //To change body of generated methods, choose Tools | Templates.
        if(msg == null) {
            System.out.println("Message NULL");
            return;
        }
        networkHandler.ctx = ctx;
        networkHandler.onMessage((CCMessage)msg);
    }
    /**
     * Phương thức được gọi khi một client kết nối đến server
     * @param ctx channel mới kết nối
     * @throws Exception 
     */
    
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx); //To change body of generated methods, choose Tools | Templates.
        System.out.println("Client vua ket noi vao server");
        ctx.channel().attr(TAttributes.CONN_STATUS_KEY).set(TAttributes.STATE_HAVECONNECTED);
        ByteBuf policyContentByteBuf = getPolicyFileContent();
        ctx.writeAndFlush(policyContentByteBuf);
    }
    
    private ByteBuf getPolicyFileContent() throws Exception {
        String contentString;
        contentString = "<?xml version=\"1.0\"?>" + NEWLINE +
     "<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">" + NEWLINE +
     "" + NEWLINE +
     "<!-- Policy file for xmlsocket://socks.example.com -->" + NEWLINE +
     "<cross-domain-policy> " + NEWLINE +
     "" + NEWLINE +
     "   <!-- This is a master socket policy file -->" + NEWLINE +
     "   <!-- No other socket policies on the host will be permitted -->" + NEWLINE +
     "   <site-control permitted-cross-domain-policies=\"master-only\"/>" + NEWLINE +
     "" + NEWLINE +
     "   <!-- Instead of setting to-ports=\"*\", administrator's can use ranges and commas -->" + NEWLINE +
     "   <allow-access-from domain=\"*\" to-ports=\"8080\" />" + NEWLINE +
     "" + NEWLINE +
     "</cross-domain-policy>" + NEWLINE;
        ByteBuf retByteBuf = null;
        retByteBuf = Unpooled.copiedBuffer(contentString, CharsetUtil.US_ASCII);
        return retByteBuf;
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause); //To change body of generated methods, choose Tools | Templates.
        ctx.close();
    }

    /**
     * Phương thức được gọi khi một client ngắt kết nối đến server
     * @param ctx channel bị ngắt kết nối
     * @throws Exception 
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx); //To change body of generated methods, choose Tools | Templates.
        System.out.println("Client da ngat ket noi");
    }    
}
